<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">    
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm">
                    <el-form-item label="机构名称" :label-width="formLabelWidth" prop="name">
                        <el-input type="textarea" v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
            </el-dialog>
            <div class="btn-div">
              <el-button size="small" type="primary" @click="downloadTem" style="margin-right: 10px;height: 32px">
                下载通讯录模板
              </el-button>
              <el-upload
                  action=""
                  class="avatar-uploader" 
                  ref="upload"
                  accept="csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                  :http-request="upload"
                  :show-file-list="false" 
                  v-loading.fullscreen.lock="fullscreenLoading" 
              >
                  <el-button size="small" type="primary">导入通讯录模板</el-button>
              </el-upload>
            </div>
        </div>
        <div>
          <el-table :data="tableData" border fit style="width: 100%">
              <el-table-column prop="user_name" label="姓名" show-overflow-tooltip>
              </el-table-column>
              <el-table-column prop="user_tel" label="联系方式" show-overflow-tooltip>
              </el-table-column>
              <el-table-column fixed="right" label="操作">
                  <template slot-scope="scope">
                      <el-button type="text" size="small" @click="deleAddressBook(scope.row)">删除</el-button>
                  </template>
              </el-table-column>
          </el-table>
        </div>
        <div class="block">
            <el-pagination
            layout="total, prev, pager, next, jumper" :total="tableData.length">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { addressBook } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { filterObj } from './../../utils/local.js'
const { importApi, listApi, deleteApi } = addressBook
export default {
    data() {
        return {
            value: '',
            addForm: {},
            dialogTableVisible: false,
            dialogFormVisible: false,
            fullscreenLoading: false,
            formLabelWidth: '120px',
            title: '',
        }
    },
    created() {
      this.getList()
    },
    computed: mapState({
        tableData: state => state.AddressBook.list
    }),
    methods: {
        ...mapActions(['saveList']),
        getList() {
          listApi().then(res => {
            if (res.code === '000000') {
              this.saveList(res)
            } else {
              this.$message.error(err.message)
            }
          }, () => {
            this.$message.error('网络错误')
          })
        },
        deleAddressBook(row) {
            this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteApi({ ids: row.id })
                    .then(res => {
                        if (res.code === '000000') {
                            this.getList()
                            this.$message.info('删除成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //下载
        downloadTem() {
          let link = document.createElement('a')
          link.href = 'http://www.yuwubao.cn:8780/zhiku/file/address_list.xlsx'
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        },
        //上传
        upload(file) {
          let formData = new FormData()
          formData.append('file', file.file);
          importApi(formData, res => {
              this.fullscreenLoading = false
              if (res.code === '000000') {
                  this.getList()
                  this.$message.success('导入成功')
              } else {
                  this.$message.error(res.message)
              }
          }, err => {
              this.fullscreenLoading = false
          })
        },
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
.btn-div {
  display: flex;
}
</style>